package com.huabo.audit.util;

import com.hbfk.entity.DealUserToken;
import com.hbfk.entity.TblStaffUtil;
import com.huabo.audit.common.ResultCode;
import com.huabo.audit.exception.CommercialException;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;

/**
 *@ClassName OrgInfoUtils
 *@Description 组织信息工具类
 *@Author ZiYao
 *@Date 2022/4/14 13:50
 *@Version 1.0
 **/
public class OrgInfoUtils {
    /**
     *@Author ZiYao
     *Description 根据token获取组织id
     *Date 13:46 2022/4/14
     *Param token
     *return BigDecimal
     **/
    public static BigDecimal getBigDecimal(String token) {
        //通过前端传token来获取组织信息
        TblStaffUtil staff;
        try {
            staff = DealUserToken.parseUserToken(token);
        } catch (Exception e) {
            throw new CommercialException(ResultCode.BIZ_ERROR,"用户已失效！");
        }
        if(staff == null) {
            throw new CommercialException(ResultCode.BIZ_ERROR,"用户已失效！");
        }
        BigDecimal pid = staff.getCurrentOrg().getOrgid();
        return pid;
    }
    public static TblStaffUtil getTblStaffUtil(String token) {
        //通过前端传token来获取组织信息
        TblStaffUtil staff;
        try {
            staff = DealUserToken.parseUserToken(token);
        } catch (Exception e) {
            throw new CommercialException(ResultCode.BIZ_ERROR,"用户已失效！");
        }
        if(staff == null) {
            throw new CommercialException(ResultCode.BIZ_ERROR,"用户已失效！");
        }
        return staff;
    }

    /**
     * 根据token获取用户id
     * @param token
     * @return
     */
    public static BigDecimal getTblStaffIdByToken(String token) {
        //检查token
        if (StringUtils.isBlank(token)) {
            throw new CommercialException(ResultCode.BIZ_ERROR, "系统错误，请联系管理员！");
        }
        BigDecimal tblStaffId;
        try {
            TblStaffUtil tblStaffUtil = DealUserToken.parseUserToken(token);
            tblStaffId = tblStaffUtil.getStaffid();
        } catch (Exception e) {
            throw new CommercialException(ResultCode.BIZ_ERROR,"用户已失效！");
        }
        return tblStaffId;
    }


}
